//
//  UIViewController+RNOLExtension.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/2.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "UIViewController+RNOLExtension.h"
#import "RNOLLoginViewController.h"

@implementation UIViewController (RNOLExtension)


#pragma mark- 弹出登录控制器
- (void)showLoginViewController{
    RNOLLoginViewController *loginVC = [[RNOLLoginViewController alloc] init];
    RNOLNavigationController *loginNavVC = [[RNOLNavigationController alloc] initWithRootViewController: loginVC];
    [self presentViewController:loginNavVC animated:YES completion:^{
        
    }];
}

#pragma mark - 无传参的push页面
- (void)pushPageWithClass:(Class)cla{
    UIViewController *VC = (UIViewController *)[[cla alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}


///web
- (void)skipWebPageWithHtmlLink:(NSString *)url title:(NSString *)title{
    RNOLWebViewController *webVC = [[RNOLWebViewController alloc] initWithURLString: url navgationBarTitle: title];
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)skipWebPageWithHtmlLink:(NSString *)url title:(NSString *)title methodType:(RNOLWebRequestType)type parmsDict:(NSDictionary *)paramsDict{
    RNOLWebViewController *webVC = [[RNOLWebViewController alloc] initWithMethodType: type parameters:paramsDict url:url title: title];
    [self.navigationController pushViewController:webVC animated:YES];
}


- (void)setupNavBarTitleViewWithText:(NSString *)text {
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 35)];
    titleLable.textColor = [UIColor whiteColor];
    titleLable.font = KFontSize(17);
    titleLable.text = text;
    titleLable.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLable;
}

#pragma mark- 添加导航栏右侧的按钮
- (void)setRightItemButtons:(NSArray *)buttons{
    NSMutableArray *itemButtons = [NSMutableArray array];
    [buttons enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL * _Nonnull stop) {
        UIBarButtonItem *itemButton = [[UIBarButtonItem alloc]initWithCustomView:button];
        [itemButtons addObject:itemButton];
    }];
    self.navigationItem.rightBarButtonItems = itemButtons;
}

#pragma mark- 添加左边返回按钮
- (void)addNavgationLeftBackButton{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 44, 44);
    leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
    UIImage *leftButtonImg = [UIImage imageNamed:@"rnol_navgaition_leftItem_back"];
    [leftButton setImage: leftButtonImg
                forState:UIControlStateNormal];
    //        leftButton.backgroundColor = [UIColor redColor];
    [leftButton addTarget:self action:@selector(backPopVCAction) forControlEvents:UIControlEventTouchUpInside];
    // 修改导航栏左边的item
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
}

- (void)backPopVCAction{

}



- (void)setupNavBarRightButtonWithTitle:(NSString *)title {
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.width = 44;
    rightButton.height = 30;
    rightButton.x = SCREEN_WIDTH - rightButton.width;
    rightButton.y = 0;
    rightButton.titleLabel.font = KFontSize(17);
    rightButton.touchAreaInsets = UIEdgeInsetsMake(-10, -40, 0, -20);
    [rightButton setTitle:title forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(navRightAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void)navRightAction:(UIButton *)button{
}

- (void)setupNavBarLeftButtonWithTitle:(NSString *)title {
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.width = 44;
    leftButton.height = 30;
    leftButton.x = 0;
    leftButton.y = 0;
    leftButton.titleLabel.font = KFontSize(17);
    leftButton.touchAreaInsets = UIEdgeInsetsMake(0, -10, -10, -20);
    [leftButton setTitle:title forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(navLeftAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
}

- (void)navLeftAction:(UIButton *)button{
}


- (void)addNavLeftButtonWithImage:(NSString *)imageName {
    UIBarButtonItem *leftBarItme = [UIBarButtonItem initWithNormalImage:imageName target:self action:@selector(leftBarButtonAction)];
    self.navigationItem.leftBarButtonItem = leftBarItme;
}

- (void)addNavRightButtonWithImage:(NSString *)imageName {
    UIBarButtonItem *rightBarItme = [UIBarButtonItem initWithNormalImage:imageName target:self action:@selector(rightBarButtonAction)];
    self.navigationItem.rightBarButtonItem = rightBarItme;
}

- (void)leftBarButtonAction {
    
}

- (void)rightBarButtonAction {
    
}


#pragma mark- 后退多少步
- (void)goBackByBackStep:(NSInteger)step animated:(BOOL)animated{
    if (step == 0) {
        return;
    }
    if (step == 1) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    NSArray *viewControllers = [self.navigationController viewControllers];
    if (step >=viewControllers.count) {
        [self.navigationController popToRootViewControllerAnimated:animated];
        return;
    }
    UIViewController *toController = viewControllers[viewControllers.count-step-1];
    [self.navigationController popToViewController:toController animated:animated];
}


//设置状态栏颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}



#pragma mark- 提示错误信息
- (void)showErrorMessageWithText:(NSString*)text{
    [SVProgressHUD showErrorWithStatus:text];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
}

#pragma mark- 提示成功信息
- (void)showSuccessMessageWithText:(NSString*)text{
    [SVProgressHUD showSuccessWithStatus:text];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
}

#pragma mark- 提示信息
- (void)showStatusTipWithText:(NSString *)text {
    [SVProgressHUD showWithStatus: text];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
}

#pragma mark- 移除导航栏
- (void)wgb_setClearNavgationBar{
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    self.navigationController.navigationBar.tintColor = [UIColor clearColor];
    self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    self.navigationController.navigationBar.translucent = YES; //透明
}

#pragma mark - 重新设置导航栏
-(void)wgb_resetNavgationBar {
    UINavigationBar * navigationBar = [UINavigationBar appearance];
    navigationBar.barTintColor = kAppMainColor;
    navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : KFontSize(15)};
    [navigationBar setBackgroundImage:[UIImage createImageWithColor:kAppMainColor] forBarMetrics:UIBarMetricsDefault];
    navigationBar.shadowImage = [UIImage createImageWithColor:kAppMainColor];
    navigationBar.tintColor = kAppMainColor;
    navigationBar.backgroundColor = kAppMainColor;
    navigationBar.translucent = NO; 
}


/**
 自定义弹窗相关
 */
#pragma mark- 大标题 左右俩按钮的wgbStyle风格弹窗
- (void)wgbStyle_alertWithTitle:(NSString *)title
              cancelButtonTitle:(NSString *)cancelTitle
             confirmButtonTitle:(NSString *)confirmTitle
                   cancelAction:(void(^)(void))cancelCallBackBlock
                  comfirmAction:(void(^)(void))confirmCompeteBlock {
    WGBCustomPopUpView *alert = [[WGBCustomPopUpView alloc] initWithFrame: CGRectMake(0,0, KWIDTH , KHIGHT)];
    UIView *bgview = [[UIView alloc] init];
    bgview.userInteractionEnabled = YES;
    bgview.backgroundColor = KWhiteColor;
    CGFloat rate = (320.0 - 56.0)/320.0;
    CGFloat viewWidth = rate * KWIDTH;
    CGFloat viewHeight = 178.0;
    bgview.width = viewWidth;
    bgview.height = viewHeight;
    bgview.layer.cornerRadius = 4;
    
    UILabel *remindLab = [[UILabel alloc] init];
//    remindLab.isCopyable = YES;
    remindLab.x = 10;
    remindLab.y = 40;
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18]};
    CGSize retSize = [title boundingRectWithSize: CGSizeMake(bgview.width-20, HUGE)     options:\
                      NSStringDrawingTruncatesLastVisibleLine |
                      NSStringDrawingUsesLineFragmentOrigin |
                      NSStringDrawingUsesFontLeading
                                      attributes:attribute
                                         context:nil].size;
    CGFloat textHight = retSize.height;
    remindLab.width = bgview.width-20;
    remindLab.height = textHight;
    remindLab.text = title;
    remindLab.numberOfLines = 0;
    remindLab.textAlignment = NSTextAlignmentCenter;
    remindLab.font =  [UIFont boldSystemFontOfSize:18];
    [bgview addSubview:remindLab];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.titleLabel.font = kFont(15);
    cancelButton.width = (bgview.width - 20 * 2 -10) / 2;
    cancelButton.x = 20;
    cancelButton.y = CGRectGetMaxY(remindLab.frame) + 40;
    cancelButton.height = 36;
    cancelButton.backgroundColor = [UIColor clearColor];
    cancelButton.layer.borderColor = [UIColor colorWithHexString:@"e0e0e0"].CGColor;
    cancelButton.layer.borderWidth = 0.9;
    [bgview addSubview:cancelButton];
    [cancelButton setTitle:cancelTitle forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor colorWithHexString:@"999999"] forState:UIControlStateNormal];
    [cancelButton addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [alert dismiss];
        !cancelCallBackBlock? : cancelCallBackBlock();
    }];
    cancelButton.layer.cornerRadius = 4;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmButton.titleLabel.font = kFont(15);
    confirmButton.x = bgview.width - 20 - cancelButton.width;
    confirmButton.y = cancelButton.y;
    confirmButton.width = cancelButton.width;
    confirmButton.height = cancelButton.height;
    confirmButton.backgroundColor = kAppMainColor;
    [confirmButton setTitleColor:KWhiteColor forState:UIControlStateNormal];
    [confirmButton setTitle:confirmTitle forState:UIControlStateNormal];
    confirmButton.layer.cornerRadius = 4;
    bgview.height = CGRectGetMaxY(confirmButton.frame)+20;
    [confirmButton addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [alert dismiss];
        !confirmCompeteBlock? : confirmCompeteBlock();
    }];
    
    [bgview addSubview:confirmButton];
    alert.contentView = bgview;
    [alert show];
}

// [取消]-[确认] 弹框
- (void)customAlertWithTitle:(NSString *)title
                cancelAction:(void(^)(void))cancelCallBackBlock
               comfirmAction:(void(^)(void))confirmCompeteBlock{
    [self wgbStyle_alertWithTitle:title cancelButtonTitle:@"取消" confirmButtonTitle:@"确认" cancelAction: cancelCallBackBlock comfirmAction: confirmCompeteBlock];
}




@end
